internals:windows:stepbystepbuild

Build your own PHP on Windows

This tutorial concerns building PHP before 7.2. To build PHP 7.2+, please refer to the newer documentation.

Before you Begin

Building PHP on Windows will require three things

  1. A properly set up build environment, including a compiler with the right SDK's and some binary tools used by the build system
  2. Prebuilt libraries and headers for third party libraries that PHP uses in the correct location
  3. The PHP source

Compiler

Requirements

PHP officially supports building with Microsoft's Visual C++ compilers. MinGW and other compilers are NOT supported or even known to work. The free Express and Developer editions can also be used. For more information and how to get the compiler see the supported versions. The following VC versions are supported:

  • Visual C++ 9.0 (Visual Studio 2008 or Visual C++ 2008) for PHP 5.4.
  • Visual C++ 11.0 (Visual Studio 2012) for PHP 5.5 or 5.6.
  • Visual C++ 14.0 (Visual Studio 2015) for PHP 7.0+.

If using VC9, you will also need the Windows SDK 6.1 (see this page for additional info on the SDK)

Setup

Command prompt

Each version of Visual Studio (or Windows SDK) provides a command prompt environment for compiling (available from the Start Menu group).

  • If compiling PHP 5.4, open the “Windows SDK 6.1 shell” and execute the following command in it:
    setenv /x86 /xp /release
  • If compiling PHP 5.5 or 5.6 open the “VS2012 x86 Native Tools Command Prompt”
  • If compiling PHP 7.0+ open either the “VS2015 x64 Native Tools Command Prompt” or the “VS2015 x86 Native Tools Command Prompt”.

All commands in the rest of this document should be run in the appropriate command prompt.

Download prerequisites

  1. Get the PHP source, there are two alternatives:
  2. Get the binary tools:
  3. Get the libraries on which PHP depends:
    • download the packaged deps from http://windows.php.net/downloads/php-sdk/
    • dependency archives are named deps-<php version>-<vc version>-<architecture>.7z
    • chose the archive which matches your desired PHP version and architecture, for example deps-5.6-vc11-x86.7z
    • Note: This should include the libraries needed to build most the core extensions. However, some other extensions may need additional libraries, header files and helper apps. See libs, fetch the version you need and extract the archive into the deps directory (see below).

Setup the build directory

This should be done only once before building PHP.

  1. Create the build directory c:\php-sdk
  2. Unpack the binary tools archive into this directory, it should contain three sub-directories: bin, script and share
  3. Open the command prompt and enter the build directory:
    cd c:\php-sdk\
  4. Run the buildtree batch script which will create the desired directory structure:
    bin\phpsdk_buildtree.bat phpdev
  5. The buildtree script hasn't been updated for newer versions of VC++ so:
    • If compiling for VC11: copy C:\php-sdk\phpdev\vc9 to C:\php-sdk\phpdev\vc11
    • If compiling for VC14: copy C:\php-sdk\phpdev\vc9 to C:\php-sdk\phpdev\vc14
  6. Extract the PHP source code to C:\php-sdk\phpdev\vc##\x##, where:
    • vc## is the compiler version you are using (vc9, vc11 or vc14)
    • x## is your architecture (x86 or x64)
    • For example: C:\php-sdk\phpdev\vc11\x86\php-5.6.4-src
  7. In the same directory where you extracted the PHP source there is a deps directory. Here you will need to extract the libraries required to build PHP, which you downloaded in the perevious step (the deps-*.7z archive).

Compile

  1. Open the command prompt and enter the build directory:
    cd c:\php-sdk\
  2. Set up the build environment variables:
    bin\phpsdk_setvars.bat
  3. Change directory to the location of your PHP source code, e.g.
    cd C:\php-sdk\phpdev\vc11\x86\php-5.6.4-src
  4. Run:
    buildconf
  5. To get an overview of the compiling flags:
    configure --help
  6. Create your configure command:
    configure --disable-all --enable-cli --enable-$remains
  7. To build PHP, run:
    nmake
  8. If you want the resulting PHP builds and extensions to be zipped, after 'nmake' also run:
    nmake snap
  9. The compiled PHP is now under “C:\php-sdk\phpdev\vcXX\x86\php-source-directory\Release_TS”. If you ran 'nmake snap' the zip file will also be here.
    • If you compiled with “--disable-zts” the compiled PHP will be under “C:\php-sdk\phpdev\vcXX\x86\php-source-directory\Release”

Recompile after you have done some changes

  1. Clean up old compiled binaries
    nmake clean
  2. If you need to update the 'configure' script
    buildconf --force
  3. Create your makefile: see release
    configure --disable-all --enable-cli --enable-$remains
  4. Compile
    nmake

Building PECL extensions

Many PECL extensions already have prebuilt DLLs which can be downloaded from the extension page on the PECL web site. For example, on the APCu extension page, there are *DLL* links next to the release archive link. They lead you to a page where you can download the compiled extension for your desired PHP version and architecture.

To build a PECL extension (for example APCu):

  1. Open the extension's page on PECL ( APCu)
  2. Download the extension source either by:
    • downloading a source archive
    • fetching the source from the extension's repository (link can be found under *Browse Source*)
  3. Create a directory named pecl on the same level as your PHP source directory, e.g. C:\php-sdk\phpdev\vc11\x86\pecl
  4. Extract or clone the extension source code to the pecl directory
    • if cloning, clone to a subdirectory, e.g. C:\php-sdk\phpdev\vc11\x86\pecl\apcu
    • source code archive should already contain a subdirectory named e.g. apcu-4.0.7
  5. Open a command prompt, run the setvars script, and enter your PHP source directory
  6. Rebuild the configure script by running:
    buildconf
  7. Executing configure --help should now contain an option to enable APCu
    --enable-apcu    Whether to enable APCu support
  8. Configure and build:
    configure --disable-all --enable-cli --enable-apcu
    nmake
  9. Test the binary with a php -m command, to make sure APCu is loaded

At this point, your compiled extension will be located in the release directory.

Additional Links:

LDAP Support Notes

  1. Add “--with-ldap” to your configure
  2. Make sure to package your generated binaries with libsasl.dll, which will not be built through this process - you can find it inside your C:\php-sdk\php53dev\vc9\x86\deps\bin\ folder, or you can download it from one of the PHP official binary packages

Pitfalls

fatal error C1900: [] mismatch between 'P1' version '200801116' and 'P2' version '20070207'

If you are using Visual Studio 2008 Express and Platform SDK 6.1 make sure you have the latest versions and check for version incompatibilities. There are known issues with old versions of VS 2008 Express and Platform SDK 6.1.

The order in which you install the Visual Studio 2008 Express Edition and Platform SDK 6.1 is important due to the issue reported in Windows SDK for Windows Server 2008 and .NET Framework 3.5 Overview, Windows SDK for Server 2008 (v6.1) overwrites VS2008 SP1 CRT files and Installing Windows SDK for Server 2008 (v6.1) after VS2008 SP1 causes conflicts with Security Update (KB971092). Install the SDK first, with any of its service packs and security updates before installing Visual Studio 2008 Express Edition.

Make sure that you are running setenv /xp /x86. For example, setenv /xp /x64 or setenv /vista /x64 causes the above error message even if you are using Visual Studio 2008 Express SP1 and Platform SDK 6.1 (60001.18000.367) on a Vista 64bit system.

Utilities

Resource Hacker

Utility to show dependency information and other assembly information from a DLL. For instance, it shows which version of the Visual C++ Runtime the DLL was linked against.

Get it here: http://angusj.com/resourcehacker/

internals/windows/stepbystepbuild.txt · Last modified: 2019/11/15 21:57 by nikic